package com.yowasa.xiuxian.engine.message.interact;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.entity.po.XiuxianShopItem;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianShopItemMapper;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@CMD(name = "/星辉商店", scope = CmdScopeEnum.ALL)
public class TransactStarLightHandler extends CmdMessageHandler {

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private ItemHelper itemHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Autowired
    private XiuxianShopItemMapper xiuxianShopItemMapper;


    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        MapEnum map = MapEnum.getById(userBO.getLocal());
        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 查询 购买 等二级指令进行后续操作");
            return;
        }
        if ("查询".equals(params.get(0))) {
            List<XiuxianShopItem> xiuxianShopItems = xiuxianShopItemMapper.selectList(new LambdaQueryWrapper<XiuxianShopItem>()
                    .eq(XiuxianShopItem::getMapId, -1)
                    .gt(XiuxianShopItem::getItemNum,0));
            String msg = "[星辉商店]\n" + itemHelper.buildBatchTransactMsg(xiuxianShopItems);

            messageHelper.replyMys(message, msg);
            return;
        }

        if ("购买".equals(params.get(0))) {
            if (params.size() < 2) {
                throw new HandlerException("请在购买后输入 物品名 数量 不输入数量默认为1");
            }
            String itemName = params.get(1);
            int num = 1;
            if (params.size() >= 3) {
                num = StrUtil.checkAndConvert2Int(params.get(2), "数量");
            }
            String text = "";
            if(itemName.equals("死之羽")){
                text = "（死之羽一次仅能购买一个，已自动修改数量）";
                num = 1;
            }
            List<XiuxianShopItem> xiuxianShopItems = xiuxianShopItemMapper.selectList(new LambdaQueryWrapper<XiuxianShopItem>()
                    .eq(XiuxianShopItem::getMapId, -1)
                    .eq(XiuxianShopItem::getItemName,itemName)
                    .gt(XiuxianShopItem::getItemNum,0).last("limit 1"));

            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(itemName);
            if (!"原粹树脂".equals(itemName) && (itemInterface == null || xiuxianShopItems.size()==0)) {
                throw new HandlerException("没有这样的物品 : " + itemName);
            }
            XiuxianShopItem item = xiuxianShopItems.get(0);

            if (item.getItemNum() < num) {
                throw new HandlerException("没有足够数量的购买物品，该物品现有上架数量为:" + item.getItemNum());
            }
            Integer cost = item.getPrice() * num;
            XiuXianExtend byAccountId = extendHelper.getByAccountId(userBO.getId());
            if (byAccountId.getStarlight() < cost) {
                throw new HandlerException("你身上的星辉不足，需要:" + cost);
            }
            //扣钱
            byAccountId.setStarlight(byAccountId.getStarlight() - cost);
            extendHelper.updateExtendById(byAccountId);
            //物品增加
            if("原粹树脂".equals(item.getItemName())){
                byAccountId.setResin(byAccountId.getResin() + num);
                extendHelper.updateExtendById(byAccountId);
            }else {
                if(itemInterface.getType().getId() == 3){
                    itemHelper.addEquip(userBO,itemInterface,num,2);
                }else {
                    itemHelper.addItem(userBO,itemInterface,num);
                }
            }
            //商店物品减少
            item.setItemNum(item.getItemNum() - num);
            xiuxianShopItemMapper.updateById(item);

            userHelper.saveUserInfo(userBO);
            messageHelper.replyMys(message, "购买成功"+text);
            return;
        }
    }
    public String desc(){
        return new StringBuffer().append("使用星辉进行交易的商店，可以在任何地点唤出，每天晚上12点会补货\n")
                .toString();

    }

}
